package sunger.net.org.dropmenu.tab.widget;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * MsgView
 */
public class MsgComponent extends Text implements ComponentContainer.ArrangeListener {
    private Context context;
    private ShapeElement gdBackground = new ShapeElement();
    private int backgroundColor = Color.TRANSPARENT.getValue();
    private int cornerRadius = 0;
    private int strokeWidth = 0;
    private int strokeColor = Color.TRANSPARENT.getValue();
    private boolean isRadiusHalfHeight = false;
    private boolean isWidthHeightEqual = false;

    /**
     * 构造器
     *
     * @param context 上下文
     * @param attrs 属性
     */
    public MsgComponent(Context context, AttrSet attrs) {
        super(context, attrs);
        this.context = context;
        obtainAttributes(context, attrs);
    }

    /**
     * 构造器
     *
     * @param context 上下文
     * @param attrs 属性
     * @param defStyleAttr defStyleAttr
     */
    public MsgComponent(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        obtainAttributes(context, attrs);
    }

    private void obtainAttributes(Context context, AttrSet attrs) {
        backgroundColor = attrs.getAttr("mv_backgroundColor").get().getColorValue().getValue();
        cornerRadius = attrs.getAttr("mv_cornerRadius").get().getIntegerValue();
        strokeWidth = attrs.getAttr("mv_strokeWidth").get().getIntegerValue();
        strokeColor = attrs.getAttr("mv_strokeColor").get().getColorValue().getValue();
        isRadiusHalfHeight = attrs.getAttr("mv_isRadiusHalfHeight").get().getBoolValue();
        isWidthHeightEqual = attrs.getAttr("mv_isWidthHeightEqual").get().getBoolValue();
    }

    /**
     * 设置背景色
     *
     * @param backgroundColor 颜色
     */
    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
        setBgSelector();
    }

    /**
     * 设置radius
     *
     * @param cornerRadius cornerRadius
     */
    public void setCornerRadius(int cornerRadius) {
        this.cornerRadius = dp2px(cornerRadius);
        setBgSelector();
    }

    /**
     * setStrokeWidth
     *
     * @param strokeWidth strokeWidth
     */
    public void setStrokeWidth(int strokeWidth) {
        this.strokeWidth = dp2px(strokeWidth);
        setBgSelector();
    }

    /**
     * setStrokeColor
     *
     * @param strokeColor strokeColor
     */
    public void setStrokeColor(int strokeColor) {
        this.strokeColor = strokeColor;
        setBgSelector();
    }

    /**
     * setIsRadiusHalfHeight
     *
     * @param isRadiusHalfHeight isRadiusHalfHeight
     */
    public void setIsRadiusHalfHeight(boolean isRadiusHalfHeight) {
        this.isRadiusHalfHeight = isRadiusHalfHeight;
        setBgSelector();
    }

    /**
     * setIsWidthHeightEqual
     *
     * @param isWidthHeightEqual isWidthHeightEqual
     */
    public void setIsWidthHeightEqual(boolean isWidthHeightEqual) {
        this.isWidthHeightEqual = isWidthHeightEqual;
        setBgSelector();
    }

    /**
     * getBackgroundColor
     *
     * @return color
     */
    public int getBackgroundColor() {
        return backgroundColor;
    }

    /**
     * getCornerRadius
     *
     * @return radius
     */
    public int getCornerRadius() {
        return cornerRadius;
    }

    /**
     * getStrokeWidth
     *
     * @return StrokeWidth
     */
    public int getStrokeWidth() {
        return strokeWidth;
    }

    /**
     * getStrokeColor
     *
     * @return strokeColor
     */
    public int getStrokeColor() {
        return strokeColor;
    }

    /**
     * isRadiusHalfHeight
     *
     * @return isRadiusHalfHeight
     */
    public boolean isRadiusHalfHeight() {
        return isRadiusHalfHeight;
    }

    /**
     * isWidthHeightEqual
     *
     * @return isWidthHeightEqual
     */
    public boolean isWidthHeightEqual() {
        return isWidthHeightEqual;
    }

    /**
     * dp2px
     *
     * @param dp dp
     * @return int
     */
    protected int dp2px(float dp) {
        final double scale = context.getResourceManager().getDeviceCapability().screenDensity;
        return (int) (dp * scale + 0.5f);
    }

    /**
     * sp2px
     *
     * @param sp sp
     * @return int
     */
    protected int sp2px(float sp) {
        final double scale = context.getResourceManager().getDeviceCapability().screenDensity;
        return (int) (sp * scale + 0.5f);
    }

    private void setDrawable(ShapeElement gd, int color, int strokeColor) {
        gd.setRgbColor(new RgbColor(color));
        gd.setCornerRadius(cornerRadius);
        gd.setStroke(strokeWidth, new RgbColor(strokeColor));
        setBackground(gd);
    }

    /**
     * setBgSelector
     */
    public void setBgSelector() {
        setDrawable(gdBackground, backgroundColor, strokeColor);
    }

    @Override
    public boolean onArrange(int i, int i1, int i2, int i3) {
        if (isRadiusHalfHeight()) {
            setCornerRadius(getHeight() / 2);
        } else {
            setBgSelector();
        }
        return true;
    }
}
